import * as Blockly from 'blockly/core';
import { Profile } from 'mixly';

const SENSOR_HUE = 40;

export const chaoshengbo2 = {
    init: function () {
        this.setColour(SENSOR_HUE);
        this.appendDummyInput("")
            .appendField(Blockly.Msg.MIXLY_CHAOSHENGBO);
        this.appendDummyInput("")
            .appendField('Trig#')
            .appendField(new Blockly.FieldDropdown(Profile.default.digital), "Trig")
            .appendField('Echo#')
            .appendField(new Blockly.FieldDropdown(Profile.default.digital), "Echo");
        this.setInputsInline(true);
        this.setOutput(true, Number);
        this.setTooltip(Blockly.Msg.MIXLY_TOOLTIP_BLOCKGROUP_CHAOSHENGBO);
        // this.setFieldValue("2","Trig");
        // this.setFieldValue("4","Echo");
    }
};

//DS18B20温度传感器
export const ds18b20 = {
    init: function () {
        var UNIT = [[Blockly.Msg.MIXLY_DS18B20_C, '0'], [Blockly.Msg.MIXLY_DS18B20_F, '1']];
        this.setColour(SENSOR_HUE);
        this.appendDummyInput("")
            .appendField(Blockly.Msg.MIXLY_DS18B20)
            .appendField(new Blockly.FieldDropdown(Profile.default.digital), "PIN")
            .appendField(Blockly.Msg.MIXLY_GETTEMPERATUE)
            .appendField(new Blockly.FieldDropdown(UNIT), "UNIT");
        this.setOutput(true, Number);
        this.setTooltip(Blockly.Msg.MIXLY_TOOLTIP_DS18);
    }
};

const BMX280_VALUE_TYPE = {
    bme: [
        [Blockly.Msg.blynk_IOT_IR_TEMP, "readTemperature()"],
        [Blockly.Msg.MIXLY_Humidity, "readHumidity()"],
        [Blockly.Msg.MIXLY_Altitude, "readPressure()"],
        [Blockly.Msg.MIXLY_HEIGHT, "readAltitude(SEALEVELPRESSURE_HPA)"]
    ],
    bmp: [
        [Blockly.Msg.blynk_IOT_IR_TEMP, "readTemperature()"],
        [Blockly.Msg.MIXLY_Altitude, "readPressure()"],
        [Blockly.Msg.MIXLY_HEIGHT, "readAltitude(SEALEVELPRESSURE_HPA)"]
    ]
};

//BMX280
export const bmx280_read_data = {
    init: function () {
        this.setColour(SENSOR_HUE);
        this.appendDummyInput()
            .appendField(Blockly.Msg.MIXLY_SERIAL_READ)
            .appendField(new Blockly.FieldDropdown([["BME280", "bme"], ["BMP280", "bmp"]]), 'TYPE');
        this.appendValueInput("address")
            .appendField(Blockly.Msg.MIXLY_LCD_ADDRESS);
        this.appendDummyInput()
            .appendField(Blockly.Msg.MIXLY_GET)
            .appendField(new Blockly.FieldDependentDropdown("TYPE", BMX280_VALUE_TYPE, BMX280_VALUE_TYPE['bme']), 'BME_TYPE');
        this.appendDummyInput()
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(' ' + Blockly.Msg.CONTROLS_FOR_INPUT_WITH)
            .appendField(new Blockly.FieldDropdown([
                ["I2C1", "Wire1"],
                ["I2C1(" + Blockly.Msg.MIXLY_STM32_SPI_REMAP + ")", "remap_I2C_1"],
                ["I2C2", "Wire2"]
            ]), "I2C_TYPE");
        this.setOutput(true, null);
        this.setInputsInline(true);
        this.setTooltip("");
        this.setHelpUrl("");
    }
};

// SHT20传感器
export const sht20_get_data = {
    init: function () {
        this.setColour(SENSOR_HUE);
        this.appendDummyInput("")
            .appendField("SHT20" + Blockly.Msg.MIXLY_DHT11_T_H);
        this.appendDummyInput("")
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.MIXLY_TEMPERATURE, "readTemperature()"],
                [Blockly.Msg.MIXLY_Humidity, "readHumidity()"],
            ]), "SHT20_TYPE");
        this.appendDummyInput()
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(' ' + Blockly.Msg.CONTROLS_FOR_INPUT_WITH)
            .appendField(new Blockly.FieldDropdown([
                ["I2C1", "Wire1"],
                ["I2C1(" + Blockly.Msg.MIXLY_STM32_SPI_REMAP + ")", "remap_I2C_1"],
                ["I2C2", "Wire2"]
            ]), "I2C_TYPE");
        this.setInputsInline(true);
        this.setOutput(true);
        this.setTooltip();
    }
};

// 初始化TCS230颜色传感器
export const tcs230_init = {
    init: function () {
        this.appendDummyInput()
            .appendField(Blockly.Msg.MIXLY_SETUP + " TCS230");
        this.appendValueInput("tcs230_s0")
            .setCheck(null)
            .appendField("S0");
        this.appendValueInput("tcs230_s1")
            .setCheck(null)
            .appendField("S1");
        this.appendValueInput("tcs230_s2")
            .setCheck(null)
            .appendField("S2");
        this.appendValueInput("tcs230_s3")
            .setCheck(null)
            .appendField("S3");
        this.appendValueInput("tcs230_led")
            .setCheck(null)
            .appendField("LED");
        this.appendValueInput("tcs230_out")
            .setCheck(null)
            .appendField("OUT");
        this.setInputsInline(true);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(SENSOR_HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    }
};

// TCS230颜色传感器 获取RGB值
export const tcs230_Get_RGB = {
    init: function () {
        this.appendDummyInput()
            .appendField("TCS230")
            .appendField(Blockly.Msg.MIXLY_GET)
            .appendField(new Blockly.FieldDropdown([[Blockly.Msg.COLOUR_RGB_RED, "R"], [Blockly.Msg.COLOUR_RGB_GREEN, "G"], [Blockly.Msg.COLOUR_RGB_BLUE, "B"]]), "tcs230_color");
        this.setInputsInline(true);
        this.setOutput(true, null);
        this.setColour(SENSOR_HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    }
};

// MPU6050更新数据
export const mpu6050_update = {
    init: function () {
        this.setColour(SENSOR_HUE);
        this.appendDummyInput("")
            .appendField(Blockly.Msg.MIXLY_MPU6050 + Blockly.Msg.MIXLY_update_data);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setInputsInline(true);
    }
};

// MPU6050获取数据
export const mpu6050_get_data = {
    init: function () {
        this.setColour(SENSOR_HUE);
        this.appendDummyInput("")
            .appendField(Blockly.Msg.MIXLY_MPU6050);
        this.appendDummyInput("")
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.MIXLY_Accel_X, "getAccX()"],
                [Blockly.Msg.MIXLY_Accel_Y, "getAccY()"],
                [Blockly.Msg.MIXLY_Accel_Z, "getAccZ()"],
                [Blockly.Msg.MIXLY_Gyro_X, "getAngleX()"],
                [Blockly.Msg.MIXLY_Gyro_Y, "getAngleY()"],
                [Blockly.Msg.MIXLY_Gyro_Z, "getAngleZ()"],
                [Blockly.Msg.MIXLY_readTempC, "getTemp()"]
            ]), "MPU6050_TYPE");
        this.setInputsInline(true);
        this.setOutput(true);
    }
};

//DF称重模块
export const weightSensor = {
    init: function () {
        this.setColour(SENSOR_HUE);
        this.appendDummyInput("")
            .appendField("Hx711")
            .appendField(Blockly.Msg.MIXLY_WEIGHTSENSOR);
        this.appendDummyInput("")
            .appendField('Dout#')
            .appendField(new Blockly.FieldDropdown(Profile.default.digital), "DOUT")
            .appendField('SCK#')
            .appendField(new Blockly.FieldDropdown(Profile.default.digital), "SCK");
        this.appendValueInput("scale")
            .setCheck(Number)
            .appendField(Blockly.Msg.HX711_scale);
        this.setInputsInline(true);
        this.setOutput(true, Number);
        this.setTooltip(Blockly.Msg.MIXLY_TOOLTIP_BLOCKGROUP_WEIGHTSENSOR);
        // this.setFieldValue("2","DOUT");
        // this.setFieldValue("4","SCK");
    }
};

var Encoder_NO = [
    [Blockly.Msg.MIXLY_ENCODER + 1, "1"],
    [Blockly.Msg.MIXLY_ENCODER + 2, "2"],
    [Blockly.Msg.MIXLY_ENCODER + 3, "3"],
    [Blockly.Msg.MIXLY_ENCODER + 4, "4"]
];

// 旋转编码器定义
export const sensor_encoder_init = {
    init: function () {
        this.appendDummyInput()
            .appendField(Blockly.Msg.MIXLY_SETUP)
            .appendField(new Blockly.FieldDropdown(Encoder_NO), "TYPE");
        this.appendValueInput("CLK")
            .setCheck(null)
            .appendField("CLK#");
        this.appendValueInput("DT")
            .setCheck(null)
            .appendField("DT#");
        this.setInputsInline(true);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(SENSOR_HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    }
};

// 旋转编码器读取
export const sensor_encoder_get = {
    init: function () {
        this.appendDummyInput()
            .appendField(new Blockly.FieldDropdown(Encoder_NO), "TYPE")
            .appendField(Blockly.Msg.MIXLY_MICROBIT_PY_STORAGE_GET)
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.MIXLY_GPS_LOCATION, "getPosition"],
                [Blockly.Msg.MIXLY_MICROBIT_Direction, "getDirection"],
                [Blockly.Msg.MIXLY_INCREMENT, "getIncrement"],
                [Blockly.Msg.MIXLY_UPPERBOUND, "getUpperBound"],
                [Blockly.Msg.MIXLY_LOWERBOUND, "getLowerBound"]
            ]), "OPERATE_TYPE");
        this.setInputsInline(true);
        this.setOutput(true, null);
        this.setColour(SENSOR_HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    }
};

// 旋转编码器设置
export const sensor_encoder_set = {
    init: function () {
        this.appendDummyInput()
            .appendField(new Blockly.FieldDropdown(Encoder_NO), "TYPE");
        this.appendValueInput("DATA")
            .setCheck(null)
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.MIXLY_GPS_LOCATION, "resetPosition"],
                [Blockly.Msg.MIXLY_INCREMENT, "setIncrement"],
                [Blockly.Msg.MIXLY_UPPERBOUND, "setUpperBound"],
                [Blockly.Msg.MIXLY_LOWERBOUND, "setLowerBound"]
            ]), "OPERATE_TYPE")
            .appendField(Blockly.Msg.MIXLY_STAT);
        this.setInputsInline(true);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(SENSOR_HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    }
};

// 旋转编码器事件
export const sensor_encoder_handle = {
    init: function () {
        this.appendDummyInput()
            .appendField(new Blockly.FieldDropdown(Encoder_NO), "TYPE")
            .appendField(new Blockly.FieldDropdown([
                [Blockly.Msg.MIXLY_ENCODER_CHANGED, "setChangedHandler"],
                [Blockly.Msg.MIXLY_ENCODER_LEFT_ROTATION, "setLeftRotationHandler"],
                [Blockly.Msg.MIXLY_ENCODER_RIGHT_ROTATION, "setRightRotationHandler"],
                [Blockly.Msg.MIXLY_ENCODER_UPPER_OVERFLOW, "setUpperOverflowHandler"],
                [Blockly.Msg.MIXLY_ENCODER_LOWER_OVERFLOW, "setLowerOverflowHandler"]
            ]), "OPERATE_TYPE");
        this.appendStatementInput("DO")
            .setCheck(null)
            .appendField(Blockly.Msg.MIXLY_MSTIMER2_DO);
        this.setInputsInline(true);
        this.setColour(SENSOR_HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    }
};


